﻿#include "AddressBookModel.h"
#include <QDebug>

AddressBookModel::AddressBookModel(QObject *parent)
    :QAbstractItemModel(parent)
{
//    MemberInfo info1 = {QStringLiteral("阿明"), ConvertChineseUnicodeToPyt(QStringLiteral("阿明")), "111", false};
//    MemberInfo info2 = {QStringLiteral("波波"), ConvertChineseUnicodeToPyt(QStringLiteral("波波")), "111", false};
//    MemberInfo info3 = {QStringLiteral("超超"), ConvertChineseUnicodeToPyt(QStringLiteral("超超")), "111", false};
//    MemberInfo info4 = {QStringLiteral("弟弟"), ConvertChineseUnicodeToPyt(QStringLiteral("弟弟")), "111", false};
//    MemberInfo info5 = {QStringLiteral("鹅鹅"), ConvertChineseUnicodeToPyt(QStringLiteral("鹅鹅")), "111", false};
//    MemberInfo info6 = {QStringLiteral("服务"), ConvertChineseUnicodeToPyt(QStringLiteral("服务")), "111", false};
//    MemberInfo info7 = {QStringLiteral("哥哥"), ConvertChineseUnicodeToPyt(QStringLiteral("哥哥")), "111", false};
//    MemberInfo info8 = {QStringLiteral("喝水"), ConvertChineseUnicodeToPyt(QStringLiteral("喝水")), "111", false};
//    MemberInfo info9 = {QStringLiteral("姐姐"), ConvertChineseUnicodeToPyt(QStringLiteral("姐姐")), "111", false};
//    MemberInfo info10 = {QStringLiteral("看门狗"), ConvertChineseUnicodeToPyt(QStringLiteral("看门狗")), "111", false};
//    MemberInfo info11 = {QStringLiteral("铃铛"), ConvertChineseUnicodeToPyt(QStringLiteral("铃铛")), "111", false};


//    m_dataList.append(info5);
//    m_dataList.append(info2);
//    m_dataList.append(info1);
//    m_dataList.append(info3);
//    m_dataList.append(info4);
//    m_dataList.append(info6);
//    m_dataList.append(info7);
//    m_dataList.append(info8);
//    m_dataList.append(info9);
//    m_dataList.append(info10);
//    m_dataList.append(info11);

    for(int i = 0; i < 26; i ++)
    {
        MemberInfo info12 = {QString('A' + i), QString('A' + i), "111", false};
        m_dataList.append(info12);
    }
}

QModelIndex AddressBookModel::index(int row, int column, const QModelIndex &parent) const
{
    if(m_dataList.size() > 0 && row >= 0 && m_dataList.size() > row)
    {
        return createIndex(row, column, (void*)(&m_dataList.at(row)));
    }
    return QModelIndex();
}

QModelIndex AddressBookModel::parent(const QModelIndex &child) const
{
    return QModelIndex();
}

int AddressBookModel::rowCount(const QModelIndex &parent) const
{
    return m_dataList.size();
}

int AddressBookModel::columnCount(const QModelIndex &parent) const
{
    return 1;
}

QVariant AddressBookModel::data(const QModelIndex &index, int role) const
{
    if(role == Qt::UserRole + 100)
    {
        int iRow = index.row();
        return QVariant::fromValue<const MemberInfo*>(&m_dataList[iRow]);
    }

    return QVariant();
}

bool AddressBookModel::setData(const QModelIndex &index, const QVariant &value, int role)
{

    if(index.row() < m_dataList.size())
    {
        if(role == Qt::UserRole + 100)
        {
            m_dataList[index.row()] = *(value.value<MemberInfo*>());
        }
        else if(role == Qt::UserRole +101)
        {
            m_dataList[index.row()].bChecked = !m_dataList[index.row()].bChecked;
        }
    }

    return true;
}

void AddressBookModel::sort(int column, Qt::SortOrder order)
{
    // 暂时用冒泡，后面再改成二叉树
    for(int i = 0; i < m_dataList.size() - 1; i++)
    {
        for(int j = i +1; j < m_dataList.size(); j++)
        {
            if(m_dataList.at(i).strNamePinYin > m_dataList.at(j).strNamePinYin)
            {
                m_dataList.swap(i,j);
            }
        }
    }
}

QModelIndex AddressBookModel::getFirstItemByIndex(const int &index)
{
    if(m_dataList.size() > 0)
    {
        for(int i = 0; i < m_dataList.size(); i++)
        {
            if(m_dataList.at(i).strNamePinYin.at(0).toUpper() == QString('A' + index).at(0))
            {
                return createIndex(i, 0, (void*)(&m_dataList.at(i)));
            }
        }
    }

    return QModelIndex();
}

int AddressBookModel::getIndexByScrollBarValue(const int &iValue)
{
    if(m_dataList.size() > 0 && m_dataList.size() >= iValue)
    {
        return m_dataList.at(iValue).strNamePinYin.at(0).toLatin1() - 'A';
    }

    return 0;
}
